/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Courses;
import java.util.List;

/**
 *
 * @author NDH
 */
public class CourseDAO extends AbstractDAO {

    /**
     * @return a list of courses in 3 centers
     */
    public List<Courses> readAll() {
        query = session.createQuery("from Courses");
        return query.list();
    }

    /**
     * @param id an id entered to get the corrective course
     * @return Courses
     * @see Course and its relating information
     */
    public Courses readByID(int id) {
        try {
            query = session.createQuery("from Courses where courseId = :id");
            query.setParameter("id", id);
            return (Courses) query.list().get(0);
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * @return All of Courses
     * @see List of courses
     */
    public List<Courses> readLiving() {
        query = getSession().createQuery("from Courses where status >= 0");
        return query.list();
    }

    /**
     * @param Courses an string of course's fields entered to create new one
     * @return int
     * @see Courses
     */
    public int create(Courses c) {
        try {
            tx = session.beginTransaction();
            session.save(c);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @param Courses an string of course's fields entered to update edited info
     * @return int
     * @see Courses
     */
    public int update(Courses c) {
        try {
            int n = c.getCourseId();
            tx = session.beginTransaction();
            int st = c.getStatus();
            c = (Courses) session.merge(c);
            session.update(c);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * @param Courses a course entered to remove from the system
     * @return int
     * @see A list of course, except the deleted course
     */
    public int remove(Courses c) {
        try {
            tx = session.beginTransaction();
            c.setStatus(-1);
            c = (Courses) session.merge(c);
            session.update(c);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * @return List<Courses>
     * @see a list table of courses that status = 0 (inactive right now)
     */
    public List<Courses> readPre() {
        query = session.createQuery("from Courses where status = 0");
        return query.list();
    }

    /**
     * @param keyword a key word entered by user to find corrective courses
     * @return List<Courses>
     * @see A list table of courses
     */
    public List<Courses> search(String keyword) {
        query = session.createQuery("from Courses st where st.name like '%" + keyword + "%' "
                + "or st.centers.name like '%" + keyword + "%' ");
        return query.list();
    }
}
